package com.bw.ppx.frame;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.blankj.utilcode.util.ConvertUtils;
import com.bw.ppx.R;
import com.bw.ppx.frame.appconfig.AppConfig;
import com.bw.ppx.frame.entity.MainTabsEntity;
import com.google.android.material.bottomnavigation.BottomNavigationItemView;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.bottomnavigation.LabelVisibilityMode;

import java.util.List;

public class AppBottomView extends BottomNavigationView {
    int[] icons = {R.drawable.icon_tab_home,
            R.drawable.icon_tab_sofa,
            R.drawable.icon_tab_publish,
            R.drawable.icon_tab_find,
            R.drawable.icon_tab_mine};

    public AppBottomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        createItem();
    }

    //创建item方法
    @SuppressLint("RestrictedApi")
    private void createItem(){
        MainTabsEntity mainTabsEntity = AppConfig.getInstance().mainTabsEntity;
        //解析rgb颜色为对应item值
        int[] colors = {
                Color.parseColor(mainTabsEntity.activeColor),
                Color.parseColor(mainTabsEntity.inActiveColor)};
        //当前item要设置对应选中以及未选中效果->xml->selector背景选择器,java->colorstatelist
        int[][] state = new int[2][];//配置对应选中状态
        state[0] = new int[]{android.R.attr.state_selected};//选中状态
        state[1] = new int[]{-android.R.attr.state_selected};//未选中状态
        ColorStateList stateList = new ColorStateList(state,colors);
        //设置item文字以及图标选中未选中颜色
        setItemTextColor(stateList);
        setItemIconTintList(stateList);
        //根据MainTAbsEntity->tabs集合添加tab
        List<MainTabsEntity.Tabs> tabs = mainTabsEntity.tabs;
        //添加当前的mecu  item
        for (MainTabsEntity.Tabs tab : tabs) {
            //判断当前tab是否开启
            if (!tab.enable){
                continue;
            }
            int id = readDestinationId(tab.pageUrl);
            if (id == -1){
                continue;
            }
            MenuItem item = getMenu().add(0,id,tab.index,tab.title);
            item.setIcon(icons[tab.index]);
        }
        //修改吊默认上挑效果以及隐藏title效果
        int index = 0;
        for (MainTabsEntity.Tabs tab : tabs) {
            int size = ConvertUtils.dp2px(tab.size);
            BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
            BottomNavigationItemView itemVeiw = (BottomNavigationItemView) menuView.getChildAt(index);
            itemVeiw.setIconSize(size);//设置item中的图标大小
            itemVeiw.setShifting(false);
            //去除默认不显示文字问题
            menuView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
            int tintColor;
            if (TextUtils.isEmpty(tab.title)){
                tintColor = Color.parseColor("#ff678f");
            }else{
                tintColor = Color.parseColor("#666666");
            }
            itemVeiw.setIconTintList(ColorStateList.valueOf(tintColor));
            index ++;
        }
        //设置tab默认选中
        if (mainTabsEntity.selectTab>=0){
            if (mainTabsEntity.tabs.get(mainTabsEntity.selectTab).enable){
                String url = mainTabsEntity.tabs.get(mainTabsEntity.selectTab).pageUrl;
                int id = readDestinationId(url);
                setSelectedItemId(id);
            }
        }
    }
    //根据pageurl获取fragment  id方法
    private int readDestinationId(String pageUrl) {
        if (TextUtils.isEmpty(pageUrl)) {
            return -1;
        }
        int id = -1;
        id = AppConfig.getInstance().destinationMap.get(pageUrl).id;
        return id;
    }
}
